package com.fsserver.facedetection.controller;

import com.fsserver.facedetection.common.Result;
import com.fsserver.facedetection.model.FaceDetectionResult;
import com.fsserver.facedetection.model.FaceFeature;
import com.fsserver.facedetection.service.FaceDetectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 人脸检测控制器
 * 
 * @author fsserver
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/api/face")
@Api(tags = "人脸检测API")
public class FaceDetectionController {

    @Resource
    private FaceDetectionService faceDetectionService;

    @PostMapping(value = "/detect", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "基础人脸检测", notes = "检测图片中的人脸位置信息")
    public Result<FaceDetectionResult> detectFaces(
            @ApiParam(value = "图片文件", required = true)
            @RequestParam("image") MultipartFile imageFile) {
        
        try {
            log.info("开始进行基础人脸检测，文件名: {}", imageFile.getOriginalFilename());
            
            if (imageFile.isEmpty()) {
                return Result.error("图片文件不能为空");
            }
            
            FaceDetectionResult result = faceDetectionService.detectFaces(imageFile);
            return Result.success(result, "人脸检测成功");
            
        } catch (Exception e) {
            log.error("人脸检测失败", e);
            return Result.error("人脸检测失败: " + e.getMessage());
        }
    }

    @PostMapping(value = "/detect/high-accuracy", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "高精度人脸检测", notes = "使用高精度算法检测图片中的人脸")
    public Result<FaceDetectionResult> detectFacesHighAccuracy(
            @ApiParam(value = "图片文件", required = true)
            @RequestParam("image") MultipartFile imageFile) {
        
        try {
            log.info("开始进行高精度人脸检测，文件名: {}", imageFile.getOriginalFilename());
            
            if (imageFile.isEmpty()) {
                return Result.error("图片文件不能为空");
            }
            
            FaceDetectionResult result = faceDetectionService.detectFacesHighAccuracy(imageFile);
            return Result.success(result, "高精度人脸检测成功");
            
        } catch (Exception e) {
            log.error("高精度人脸检测失败", e);
            return Result.error("高精度人脸检测失败: " + e.getMessage());
        }
    }

    @PostMapping(value = "/features", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "人脸特征提取", notes = "提取图片中人脸的详细特征信息")
    public Result<List<FaceFeature>> extractFaceFeatures(
            @ApiParam(value = "图片文件", required = true)
            @RequestParam("image") MultipartFile imageFile) {
        
        try {
            log.info("开始进行人脸特征提取，文件名: {}", imageFile.getOriginalFilename());
            
            if (imageFile.isEmpty()) {
                return Result.error("图片文件不能为空");
            }
            
            List<FaceFeature> features = faceDetectionService.extractFaceFeatures(imageFile);
            return Result.success(features, "人脸特征提取成功");
            
        } catch (Exception e) {
            log.error("人脸特征提取失败", e);
            return Result.error("人脸特征提取失败: " + e.getMessage());
        }
    }

    @PostMapping(value = "/compare", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "人脸比对", notes = "比较两张图片中人脸的相似度")
    public Result<Double> compareFaces(
            @ApiParam(value = "第一张图片", required = true)
            @RequestParam("image1") MultipartFile imageFile1,
            @ApiParam(value = "第二张图片", required = true)
            @RequestParam("image2") MultipartFile imageFile2) {
        
        try {
            log.info("开始进行人脸比对，文件1: {}，文件2: {}", 
                    imageFile1.getOriginalFilename(), imageFile2.getOriginalFilename());
            
            if (imageFile1.isEmpty() || imageFile2.isEmpty()) {
                return Result.error("图片文件不能为空");
            }
            
            Double similarity = faceDetectionService.compareFaces(imageFile1, imageFile2);
            return Result.success(similarity, "人脸比对成功");
            
        } catch (Exception e) {
            log.error("人脸比对失败", e);
            return Result.error("人脸比对失败: " + e.getMessage());
        }
    }

    @PostMapping(value = "/batch/detect", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "批量人脸检测", notes = "批量检测多张图片中的人脸")
    public Result<List<FaceDetectionResult>> batchDetectFaces(
            @ApiParam(value = "图片文件列表", required = true)
            @RequestParam("images") List<MultipartFile> imageFiles) {
        
        try {
            log.info("开始进行批量人脸检测，图片数量: {}", imageFiles.size());
            
            if (imageFiles.isEmpty()) {
                return Result.error("图片文件列表不能为空");
            }
            
            List<FaceDetectionResult> results = faceDetectionService.batchDetectFaces(imageFiles);
            return Result.success(results, "批量人脸检测成功");
            
        } catch (Exception e) {
            log.error("批量人脸检测失败", e);
            return Result.error("批量人脸检测失败: " + e.getMessage());
        }
    }

    @PostMapping(value = "/age-gender", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "年龄性别检测", notes = "检测图片中人脸的年龄和性别")
    public Result<List<FaceFeature>> detectAgeGender(
            @ApiParam(value = "图片文件", required = true)
            @RequestParam("image") MultipartFile imageFile) {
        
        try {
            log.info("开始进行年龄性别检测，文件名: {}", imageFile.getOriginalFilename());
            
            if (imageFile.isEmpty()) {
                return Result.error("图片文件不能为空");
            }
            
            List<FaceFeature> features = faceDetectionService.detectAgeGender(imageFile);
            return Result.success(features, "年龄性别检测成功");
            
        } catch (Exception e) {
            log.error("年龄性别检测失败", e);
            return Result.error("年龄性别检测失败: " + e.getMessage());
        }
    }

    @PostMapping(value = "/emotion", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "情绪检测", notes = "检测图片中人脸的情绪表情")
    public Result<List<FaceFeature>> detectEmotion(
            @ApiParam(value = "图片文件", required = true)
            @RequestParam("image") MultipartFile imageFile) {
        
        try {
            log.info("开始进行情绪检测，文件名: {}", imageFile.getOriginalFilename());
            
            if (imageFile.isEmpty()) {
                return Result.error("图片文件不能为空");
            }
            
            List<FaceFeature> features = faceDetectionService.detectEmotion(imageFile);
            return Result.success(features, "情绪检测成功");
            
        } catch (Exception e) {
            log.error("情绪检测失败", e);
            return Result.error("情绪检测失败: " + e.getMessage());
        }
    }

    @GetMapping("/health")
    @ApiOperation(value = "健康检查", notes = "检查人脸检测服务是否正常运行")
    public Result<String> healthCheck() {
        try {
            return Result.success("OK", "人脸检测服务运行正常");
        } catch (Exception e) {
            log.error("健康检查失败", e);
            return Result.error("服务异常: " + e.getMessage());
        }
    }
} 